var app = getApp();
var util = require("../../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sex: ['否', '是'],
    sexindex: 1,
    date: '请选择申请入驻时间',
    type: 1,
    addressList: ['公司搬迁至园区', '在园区开设分公司', '成立新公司', '其它'],
    addressIndex: 0,
    radiolist: ['未注册', '已注册'],
    currentIndex: 0,
    applySettleFileList: []
  },

  uploFile() {
    var that = this
    if (that.data.id) return
    if (that.data.applySettleFileList.length >= 9) return
    wx.showActionSheet({
      itemList: ['上传图片', '打开微信聊天会话选择文件'],
      success(res) {
        if (res.tapIndex == 0) {
          wx.chooseImage({
            count: 1, //能选择文件的数量
            success(res) {
              that.upload(res.tempFiles)
            }
          })
        } else {
          wx.chooseMessageFile({
            count: 1, //能选择文件的数量
            type: 'file',
            success(res) {
              that.upload(res.tempFiles)
            }
          })
        }
      },
      fail(res) {
        console.log(res.errMsg)
      }
    })
  },

  upload(data) {
    var that = this
    var applySettleFileList = that.data.applySettleFileList
    wx.showLoading({
      title: '上传中',
    })
    wx.uploadFile({
      url: app.DOMAIN_NAME + '/api/dfs/upload?originalFilename=' + data[0].name,
      header: {
        'Authorization': 'Bearer ' + app.Getopenid()
      },
      filePath: data[0].path,
      name: 'file',
      success(res) {
        var deta = JSON.parse(res.data)
        if (deta.code == 401) {
          wx.hideLoading()
          wx.showModal({
            confirmText: '好的',
            content: deta.msg || '身份已过期,需重登录',
            success: res => {
              wx.reLaunch({
                url: '/pages/login/login',
              })
            }
          });
        } else if (deta.code != 0) {
          wx.hideLoading()
          wx.showModal({
            confirmText: '好的',
            content: deta.msg || '服务器开小差去了，请重试',
            showCancel: false
          });
        } else if (deta.code == 0) {
          var first = deta.fileName.lastIndexOf("."); //取到文件名开始到最后一个点的长度
          var namelength = deta.fileName.length; //取到文件名长度
          var filesuffix = deta.fileName.substring(first + 1, namelength); //截取获得后缀名
          var type = ''
          switch (filesuffix) {
            case 'jpg' || 'png':
              type = 'jpg'
              break;
            case 'xls' || 'xlsx':
              type = 'xlsx'
              break;
            case 'doc' || 'docx' || 'dotx':
              type = 'word'
              break;
            case 'ppt' || 'pptx':
              type = 'ppt'
              break;
            default:
              type = '未知文件'
              break;
          }
          applySettleFileList.push({
            fileName: deta.originalFilename != "undefined" ? deta.originalFilename : deta.fileName,
            filePath: deta.fileName,
            fileSize: (data[0].size * 0.001).toFixed(2) + 'KB',
            type
          })
          that.setData({
            applySettleFileList
          })
          wx.hideLoading()
        }
      }
    })
  },

  delFile(e) {
    var index = e.currentTarget.dataset.index,
      applySettleFileList = this.data.applySettleFileList
    applySettleFileList.splice(index, 1)
    this.setData({
      applySettleFileList
    })
  },

  // 企业入园方式
  addressRadio(e) {
    this.setData({
      addressIndex: e.currentTarget.dataset.idx,
      address: e.currentTarget.dataset.idx
    })
  },

  // 申请入驻时间选择
  bindDateChange(e) {
    var time = new Date(e.detail.value).getTime();
    console.log(e.detail.value)
    this.setData({
      date: e.detail.value,
      timec: time
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(e) {
    var time = util.formatDate(new Date())
    this.setData({
      date: time
    })
    if (e.id) {
      this.setData({
        id: e.id
      })
      this.getList(e.id)
    }
  },

  getList(e) {
    var that = this
    app.AjaxRequest('get', {
      'content-type': 'application/json',
      'Authorization': 'Bearer ' + app.Getopenid()
    }, '/settle/get/' + e, {}, function (res) {
      if (res.code == 0) {
        var applySettleFileList = res.data.applySettleFileList
        applySettleFileList.forEach(item => {
          var first = item.fileName.lastIndexOf("."); //取到文件名开始到最后一个点的长度
          var namelength = item.fileName.length; //取到文件名长度
          var filesuffix = item.fileName.substring(first + 1, namelength); //截取获得后缀名
          switch (filesuffix) {
            case 'jpg' || 'png':
              item.type = 'jpg'
              break;
            case 'xls' || 'xlsx':
              item.type = 'xlsx'
              break;
            case 'doc' || 'docx' || 'dotx':
              item.type = 'word'
              break;
            case 'ppt' || 'pptx':
              item.type = 'ppt'
              break;
            default:
              item.type = '未知文件'
              break;
          }
        })
        that.setData({
          list: res.data,
          applySettleFileList,
          date: res.data.startTime,
          isComplianceIndex: res.data.isCompliance,
          addressIndex: res.data.type
        })
      }
    })
  },

  showToast(e) {
    wx.showToast({
      title: e,
      icon: 'none'
    })
  },

  fromsumb(e) {
    var that = this,
      regMobile = /^[1]([3-9])[0-9]{9}$/,
      deta = e.detail.value
    deta.isCompliance = that.data.isComplianceIndex
    deta.type = that.data.addressIndex
    deta.applyParkFileList = that.data.applyParkFileList
    deta.startTime = that.data.date
    deta.applySettleFileList = that.data.applySettleFileList
    if (!deta.userName) {
      that.showToast('请输入联系人姓名')
      return false
    }
    if (!deta.name) {
      that.showToast('请输入入驻企业名称')
      return false
    }
    if (!deta.operName) {
      that.showToast('请输入法人代表')
      return false
    }
    if (!deta.needArea) {
      that.showToast('请输入所需办公面积')
      return false
    }
    if (!deta.persons) {
      that.showToast('请输入首批入驻人数')
      return false
    }
    if (!deta.phone) {
      that.showToast('请输入联系电话')
      return false
    }
    if (!regMobile.test(deta.phone)) {
      that.showToast('联系电话格式错误')
      return false
    }
    if (!deta.email) {
      that.showToast('请输入邮箱')
      return false
    }
    app.AjaxRequest('POST', {
      'content-type': 'application/json',
      'Authorization': 'Bearer ' + app.Getopenid()
    }, '/settle/save', deta, function (res) {
      if (res.code == 0) {
        wx.showModal({
          title: '提示',
          content: '已提交成功',
          success(res) {
            if (res.confirm) {
              wx.reLaunch({
                url: '/pages/my/serviceApply/serviceApply',
              })
            }
          }
        })
      }
    })
  }
})